<?php
/**
 * 任务审核被退回处理
 * @author ice <iceinto@mallog.com.cn>
 *
 */
class TaskAuditReturnModel extends BaseModel
{

	/**
	 * 根据任务执行用户返回一个被审核退回的任务
	 * 获取当前最早被退回的审核任务
	 * 针对审核退回任务获取对应退回信息和分配信息
	 * 处理任务状态
	 * 处理执行人状态
	 * 处理任务审核退回状态
	 * @param int $tid
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function getReturnOne($tid){
		$map = array();
		$map['tid'] = $tid;
		$map['status'] = 1;
		return $this->getByWhere($map, 'id,uid,remark,create_time');
	}

	/**
	 * Get a task all audit returned
	 * @param int $tid
	 * @param int $uid
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function getOneAllReturns($tid){
		$map = array();
		$map['tid'] = $tid;
		$map['status'] = 2;
		return $this->getListByWhere($map, 'id,uid,remark,create_time,reply,reply_time,image');
	}

	/**
	 * Add one task audit return
	 * @param int $uid user id
	 * @param int $tid task id
	 * @param string $remark
	 * @return int
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function addOne($uid, $tid, $remark){
		$data = array();
		$data['create_time'] = time();
		$data['uid'] = $uid;
		$data['tid'] = $tid;
		$data['remark'] = $remark;
		return $this->add($data);
	}
	
	/**
	 * 获取当前用户规定时间审核返回情况
	 * @param int $uid
	 * @param int $y
	 * @param int $m
	 * @author ice <iceinto@mallog.com.cn>
	 */
	public function getUidCount($uid, $y = 0, $m = 0){
		$Date = new Date();
		if($y && $m){
			$Date = new Date($y.'-'.$m.'-01');
		}
		$fireTime = $Date->firstDayOfMonth()->format("%Y-%m-%d");
		$lastTime = $Date->lastDayOfMonth()->format("%Y-%m-%d");
	
		$fireTime = strtotime($fireTime);
		$lastTime = strtotime($lastTime);
	
		$map = array();
		$map['m_task_allot.uid'] = $uid;
		$map['m_task_audit_return.create_time'] = array(array('egt', $fireTime), array('elt', $lastTime));
	
		return $this->field('m_task_allot.uid,COUNT(1) AS return_count')
		->join('INNER JOIN m_task_allot ON m_task_audit_return.tid = m_task_allot.tid')
		->where($map)
		->group('m_task_allot.uid')
		->find();
	}
}